package uk.org.landeg.euler.problems;

import uk.org.landeg.euler.problems.framework.AbstractProblem;
import uk.org.landeg.euler.problems.framework.ProblemLauncher;
import uk.org.landeg.euler.problems.lib.CombinationGenerator;
import uk.org.landeg.euler.problems.lib.NumbersLib;

public class Problem038 extends AbstractProblem{
	public static void main(String[] args) {
		ProblemLauncher launcher = new ProblemLauncher(new Problem038());
		launcher.solve();
	}

	@Override
	public String getDescription() {
		// TODO Auto-generated method stub
		return "What is the largest 1 to 9 pandigital 9-digit number that can be formed as the concatenated product of an integer with (1,2, ... , n) where n  1?";
	}

	@Override
	public Integer getProblemNumber() {
		// TODO Auto-generated method stub
		return 38;
	}

	@Override
	public Object solve() {
		CombinationGenerator cg = new CombinationGenerator(new int[] {1,2,3,4,5,6,7,8,9});
		int max = (int)NumbersLib.factorial(9);
		int mult;
		int product;
		int maxProd = 0;
		String productString = "";
		boolean exclude;
		for (int l = 9 ; l < max ; l++) {
			exclude = false;
			mult = 1;
			product = 0;
			productString = "";
			do {
				productString += (l * mult);
				if (productString.contains("0")) {
					exclude = true;
					break;
				}
				mult++;
			} while (productString.length() < 9);
			
			if (!exclude && productString.length() == 9) {
				product = Integer.parseInt(productString);
				if (NumbersLib.isPandigital(product)) {
					System.out.println("Pandigital discovere : "+ product + "(" + l + ")");
					if (product > maxProd) {
						maxProd = product;
						System.out.println("");
						System.out.println("new maxima " + product + "(" + l + ")");
					}
				}
			}
			if (l > 0 && l % 100000 == 0) {
				System.out.print(".");
			}
		}
		return maxProd;
	}

}
